/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.emergency.resources.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.common.core.validate.ValidatorUtils;
import com.hopes.bizz.emergency.common.constant.enums.ParkEmergencySysDictEnum;
import com.hopes.bizz.emergency.plan.dto.ErGovernmentEmergencyPlanExcelDTO;
import com.hopes.bizz.emergency.plan.entity.ErGovernmentEmergencyPlanEntity;
import com.hopes.bizz.emergency.plan.vo.ErGovernmentEmergencyPlanExcelVO;
import com.hopes.bizz.emergency.resources.dto.ErAddressBookExcelDTO;
import com.hopes.bizz.emergency.resources.entity.ErAddressBookEntity;
import com.hopes.bizz.emergency.resources.mapper.ErAddressBookMapper;
import com.hopes.bizz.emergency.resources.service.ErAddressBookService;
import com.hopes.bizz.emergency.resources.vo.ErAddressBookExcelVO;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.admin.api.resolver.enums.AdminDictEnum;
import com.hopes.boss.admin.api.resolver.enums.SysDictEnum;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 应急通讯录
 *
 * @author cx
 * @date 2024-01-12 15:39:27
 */
@Service
public class ErAddressBookServiceImpl extends ServiceImpl<ErAddressBookMapper, ErAddressBookEntity> implements ErAddressBookService {

    @Override
    public ValidateImportBO<ErAddressBookExcelDTO> validateImport(List<ErAddressBookExcelDTO> excelDTOList) {
        ValidateImportBO<ErAddressBookExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<ErAddressBookExcelDTO> saveList = new ArrayList<>();
        int successNum = 0;
        int failedNum = 0;
        // 做校验
        for (ErAddressBookExcelDTO excelDTO : excelDTOList) {
            String validateRes = ValidatorUtils.validateEntity(excelDTO);
            if (StrUtil.isEmpty(validateRes)) {
                successNum++;
                saveList.add(excelDTO);
            } else {
                failedNum++;
                errorMessageList.add(new ErrorMessage(excelDTO.getLineNum(), new HashSet<String>() {{
                    add(validateRes);
                }}));
            }

        }

        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public ErAddressBookEntity importDtoToEntity(ErAddressBookExcelDTO excelDto) {
        ErAddressBookEntity entity = new ErAddressBookEntity();
        BeanUtils.copyProperties(excelDto, entity);
        entity.setCompanyId(Long.valueOf(DictResolver.getItemValueByLabel(AdminDictEnum.sys_company, excelDto.getCompanyId())));
        entity.setUseFlag(DictResolver.getItemValueByLabel(SysDictEnum.yes_no_type, excelDto.getUseFlag()));
        return entity;
    }

    @Override
    public ErAddressBookExcelVO exportEntityToVo(ErAddressBookEntity entity) {
        ErAddressBookExcelVO excelVO = new ErAddressBookExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        return excelVO;
    }
}